#include <windows.h>

namespace IOUtils
{
#pragma	once

#pragma warning (disable: 4786)

class MappedFile {
private:
	HANDLE hFile,hFileMapping;
	unsigned char *ptr;
	unsigned int sz;
public:
	MappedFile();
	~MappedFile();
	unsigned char *OpenForRead(const TCHAR *filename);
	bool IsOpen();
	void Close();
	unsigned int GetSize() const {
		return sz;
	}
};

class RIFFFile {
	MappedFile mapfile;
	unsigned char *file;
	unsigned int curpos;
	int stackptr;
	DWORD stack[32];
	DWORD szstack[32];
	DWORD start[32];

public:
	RIFFFile();
	bool Open(const TCHAR *filename);
	void Close();
	bool Descend(DWORD chunkname, DWORD id = 0);
	bool DescendBS(DWORD chunkname, DWORD id = 0); //byteswapped :(
	void Ascend();
	void Restart() {curpos = start[stackptr];}
	DWORD Read();
	void Seek(int offset);
	void SeekRelative(int offset) {curpos += offset;}
	int Tell() const {return curpos;}
	const void *GetChunkPtr() const;
	int GetChunkSize() const;
	void FakeOuterBlock();
};

}  // namespace